package com.wcj.test;

import com.wcj.linktable.ListNode;

import java.util.Stack;

/**
 * 输入：
 * {1,2,2,1}
 * 返回值：
 * true
 * 说明：
 * 1->2->2->1
 */
public class 判断一个链表是否为回文结构 {

    public boolean isPail (ListNode head) {
        if (head == null || head.next == null){
            return true;
        }
        Stack<Integer> stack = new Stack<>();
        ListNode temp = head;
        while (temp != null){
            stack.push(temp.val);
            temp = temp.next;
        }
        temp = head;
        while (temp != null){
            if (temp.val == stack.pop()){
                temp = temp.next;
            }else
                return false;
        }
        return true;
    }
}
